package cn.ilikes.tools.nosql.jpa.persistences.contexts.operations;

import org.slf4j.Logger;

import cn.ilikes.tools.logs.Loggerfactory;
import cn.ilikes.tools.nosql.jpa.metadatas.EntityMetaData;
import cn.ilikes.tools.nosql.jpa.metadatas.IEntityMetaDataFactory;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.AbstractContext;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.Operation.OperationType;

public abstract class ContextOperation {

	private static Logger logger = Loggerfactory.instance(ContextOperation.class);

	private OperationType operationType;

	private AbstractContext context;

	public ContextOperation(OperationType operationType, AbstractContext context) {
		super();
		this.operationType = operationType;
		this.context = context;
	}

	public String getPuName() {
		return context.getPersistenUnitEnv().getPersistenUnitName();
	}

	public OperationType getOperationType() {

		return operationType;
	}

	protected EntityMetaData selectIEntityMetaData(String puName, Class<?> clazz) {

		AbstractContext context = getContext();

		IEntityMetaDataFactory entityMetaDataFactory = context.getPersistenUnitEnv().getMetaDataManagerFactory();

		return entityMetaDataFactory.selectIEntityMetaData(clazz);
	}

	public AbstractContext getContext() {
		return context;
	}

	public boolean flush() {
		if (Loggerfactory.isdebug(logger))
			Loggerfactory.debug(logger, "PersistOperation  flush ");

		return true;
	}

	public boolean rollback() {
		if (Loggerfactory.isdebug(logger))
			Loggerfactory.debug(logger, "PersistOperation  rollback ");

		return true;
	}

	public boolean init() {
		if (Loggerfactory.isdebug(logger))
			Loggerfactory.debug(logger, "PersistOperation  init ");

		return true;
	}
}